package com.omega.framework.common.util.date;

import org.apache.commons.lang3.time.DateUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * DateUtil时间工具类
 *
 * @author Lv.
 * @date 2022/11/2 12:08
 */
public class DateUtil extends DateUtils {
    private static final long serialVersionUID = 6400302938892276139L;
    public static final int SECOND = 1000;
    public static final int MINUTE = 60000;
    public static final int HOUR = 3600000;
    public static final int DAY = 86400000;
    public static final int WEEK = 604800000;
    public static final int YEAR = 1471228928;
    public static long millionSecondsOfDay = 86400000L;
    public static final String TIME_FORMAT_DIGITAL = "yyyyMMddHHmmss";
    public static final String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String TIME_FORMAT_MINUTE = "MM-dd HH:mm";
    public static final String TIME_FORMAT_MSEL = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String TIME_FORMAT_ORACLE = "yyyy-mm-dd hh24:mi:ss";
    public static final String DATE_FORMAT = "yyyy-MM-dd";
    public static final String DATE_PATTERN = "yyyyMMdd";
    public static final String YYYY_MM = "yyyy-MM";
    public static final String TIME_FORMAT_DAY = "yyyy-MM-dd";

    public DateUtil() {
    }

    public static Date newDate(int month, int date, int year) {
        Calendar inst = Calendar.getInstance();
        inst.clear();
        inst.set(year, month - 1, date);
        return inst.getTime();
    }

    public static Date getDate(Date date, int i) {
        if (null == date) {
            return null;
        } else {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(5, i);
            return calendar.getTime();
        }
    }

    public static int compare(Date date1, Date date2) {
        return getDay(date1, date2);
    }

    public static int getDay(Date date1, Date date2) {
        if (null != date1 && null != date2) {
            date1 = getDate(date1);
            date2 = getDate(date2);
            return Long.valueOf((date2.getTime() - date1.getTime()) / millionSecondsOfDay).intValue();
        } else {
            return 0;
        }
    }

    public static Date getDate(Date date) {
        return null == date ? null : getDate(datetimeToString(date));
    }

    public static int comparte(Date date1, String data2String) {
        return getDay(date1, getDate(data2String));
    }

    public static Date getDate(String str) {
        if (null != str && !str.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

            try {
                return sdf.parse(str);
            } catch (ParseException var3) {
                var3.printStackTrace();
                return null;
            }
        } else {
            return null;
        }
    }

    public static String dateToString(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            return sdf.format(date);
        }
    }

    public static String dateToString2(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd", Locale.CHINA);
            return sdf.format(date);
        }
    }

    public static String dateToStringNew(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
            return sdf.format(date);
        }
    }

    public static String convertDate2String(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        return format.format(date);
    }

    public static Date convertString2Time(String time) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return format.parse(time);
        } catch (ParseException var3) {
            return new Date();
        }
    }

    public static String converDateToString(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

    public static String converDate2String2(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    public static String datetimeToString(Date date) {
        if (null == date) {
            return "";
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
            return sdf.format(date);
        }
    }

    public static String toStringByFormat(Date date, String format) {
        if (null != date) {
            SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
            return sdf.format(date);
        } else {
            return "";
        }
    }

    public static String formatDateToSQLString(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatDateToYMDHMS(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatDateToYMDHMS1(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatDateToMMDD(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM-dd", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatTimeToString(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatTimeHToString(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HH", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static String formatHourTimeHToString(Date srcDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("HHmm", Locale.CHINA);
        return sdf.format(srcDate);
    }

    public static Date convertStrToDate(String dateStr, String dateFormat) {
        if (dateStr != null && !dateStr.equals("")) {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);

            try {
                return sdf.parse(dateStr);
            } catch (Exception var4) {
                throw new RuntimeException("DateUtil.convertStrToDate():" + var4.getMessage());
            }
        } else {
            return null;
        }
    }

    public static String convertDateToStr(Date date, String dateFormat) {
        if (date == null) {
            return null;
        } else {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            return sdf.format(date);
        }
    }

    public static java.sql.Date convertToSqlDate(Date date) {
        if (date == null) {
            return null;
        } else {
            java.sql.Date sqlDate = new java.sql.Date(date.getTime());
            return sqlDate;
        }
    }

    public static Date forwordOrBackwordMonths(Date date, String dateFormat, int months) {
        try {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(2, months);
            Date newDate = calendar.getTime();
            return newDate;
        } catch (Exception var5) {
            throw new RuntimeException("DateUtil.convertStrToDate():" + var5.getMessage());
        }
    }

    public static Date addDate(Date date, int days) {
        if (date == null) {
            return date;
        } else {
            Locale loc = Locale.getDefault();
            GregorianCalendar cal = new GregorianCalendar(loc);
            cal.setTime(date);
            int day = cal.get(5);
            int month = cal.get(2);
            int year = cal.get(1);
            cal.set(year, month, day + days);
            return cal.getTime();
        }
    }

    public static Date addHour(Date date, int hours) {
        if (date == null) {
            return date;
        } else {
            Locale loc = Locale.getDefault();
            GregorianCalendar cal = new GregorianCalendar(loc);
            cal.setTime(date);
            int minute = cal.get(12);
            int hour = cal.get(11);
            int day = cal.get(5);
            int month = cal.get(2);
            int year = cal.get(1);
            cal.set(year, month, day, hour + hours, minute);
            return cal.getTime();
        }
    }

    public static Date addMinute(Date date, int count) {
        if (date == null) {
            return date;
        } else {
            Locale loc = Locale.getDefault();
            GregorianCalendar cal = new GregorianCalendar(loc);
            cal.setTime(date);
            int minute = cal.get(12);
            int hour = cal.get(11);
            int day = cal.get(5);
            int month = cal.get(2);
            int year = cal.get(1);
            cal.set(year, month, day, hour, minute + count);
            return cal.getTime();
        }
    }

    public static String weeksToString(String[] week) {
        if (null == week) {
            return "";
        } else {
            StringBuffer sb = new StringBuffer();
            String result = "";

            for (int i = 0; i < week.length; ++i) {
                if (i == week.length - 1) {
                    sb.append(week[i]);
                } else {
                    sb.append(week[i]).append(",");
                }

                result = sb.toString();
            }

            return result;
        }
    }

    public static Date addMonth(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, months);
        Date newDate = calendar.getTime();
        return newDate;
    }

    public static boolean between(Date date, Date start, Date end) {
        return 0 <= getDay(start, date) && 0 >= getDay(end, date);
    }

    public static boolean betweenSomeTime(String start, String end) {
        if (start != null && end != null) {
            String[] startTime = start.split(":");
            long startH = 0L;
            if (startTime.length == 2) {
                startH = (long) (Integer.valueOf(startTime[0]) * 60 + Integer.valueOf(startTime[1]));
            }

            String[] endTime = end.split(":");
            long endH = 0L;
            if (endTime.length == 2) {
                endH = (long) (Integer.valueOf(endTime[0]) * 60 + Integer.valueOf(endTime[1]));
            }

            Calendar cal = Calendar.getInstance();
            long currentH = (long) (cal.get(11) * 60 + cal.get(12));
            if (startH > endH) {
                if (currentH > startH && currentH < 1440L) {
                    return true;
                }

                if (currentH > 0L && currentH < endH) {
                    return true;
                }
            } else if (currentH > startH && currentH < endH) {
                return true;
            }

            return false;
        } else {
            return false;
        }
    }

    public static int betweenDay(String lastTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dstr = lastTime;
        if (lastTime == null) {
            dstr = "0000-00-00";
        }

        Calendar cal = Calendar.getInstance();
        long current = (long) (cal.get(1) * 12 * 31 + cal.get(2) * 31 + cal.get(5));
        long last = 0L;

        try {
            cal.setTime(sdf.parse(dstr));
            last = (long) (cal.get(1) * 12 * 31 + cal.get(2) * 31 + cal.get(5));
        } catch (ParseException var9) {
            var9.printStackTrace();
            return -1;
        }

        int t = (int) Math.abs(current - last);
        return t;
    }

    public static int betweenSecond(String lastTime) {
        Calendar cal = Calendar.getInstance();
        long currentm = (long) (cal.get(1) * 12 * 31 * 24 * 60 * 60 + cal.get(2) * 31 * 24 * 60 * 60 + cal.get(5) * 24 * 60 * 60 + cal.get(11) * 60 * 60 + cal.get(12) * 60 + cal.get(13));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dstr = lastTime;

        try {
            cal.setTime(sdf.parse(dstr));
        } catch (ParseException var9) {
            var9.printStackTrace();
        }

        long lastm = (long) (cal.get(1) * 12 * 31 * 24 * 60 * 60 + cal.get(2) * 31 * 24 * 60 * 60 + cal.get(5) * 24 * 60 * 60 + cal.get(11) * 60 * 60 + cal.get(12) * 60 + cal.get(13));
        int tm = (int) Math.abs(currentm - lastm);
        return tm;
    }

    public static String timestampToDateStr(String timeMills) {
        Long timestamp = Long.parseLong(timeMills);
        String date = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date(timestamp));
        return date;
    }

    public static Date addTime(String timePart, int number, Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (timePart.equals("HH")) {
            cal.add(10, number);
        } else if (timePart.equals("mm")) {
            cal.add(12, number);
        } else {
            if (!timePart.equals("ss")) {
                throw new IllegalArgumentException("DateUtil.addDate()方法非法参数值：" + timePart);
            }

            cal.add(13, number);
        }

        return cal.getTime();
    }

    public static int getCurrentYear() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int year = aCalendar.get(1);
        return year;
    }

    public static int getCurrentMonth() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int month = aCalendar.get(2) + 1;
        return month;
    }

    public static int getCurrentMonthDays() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int days = aCalendar.getActualMaximum(5);
        return days;
    }

    public static int getCurrentDay() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int day = aCalendar.get(5);
        return day;
    }

    public static int getCurrentHour() {
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        int hour = aCalendar.get(11);
        return hour;
    }

    public static boolean isDayInMonth(String time, int month) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTime(convertString2Time(time));
        int m = calendar.get(2) + 1;
        return m == month;
    }

    public static boolean isOverPM12(String time) {
        int TT = 12;
        Calendar lastCalendar = Calendar.getInstance(Locale.CHINA);
        lastCalendar.setTime(convertString2Time(time));
        if (betweenDay(time) > 0) {
            return true;
        } else {
            int currentDay = getCurrentDay();
            int currentHour = getCurrentHour();
            if (currentDay == lastCalendar.get(5)) {
                if (currentHour > TT && lastCalendar.get(11) < TT) {
                    return true;
                }
            } else if (currentHour >= TT) {
                return true;
            }

            return false;
        }
    }

    public static int getCurrentWeek() {
        Calendar cal = Calendar.getInstance(Locale.CHINA);
        cal.setTime(new Date());
        int w = cal.get(7) - 1;
        if (w <= 0) {
            w = 7;
        }

        return w;
    }

    public static int getTimeMillis() {
        long head = 1421000000000L;
        long t = System.currentTimeMillis() - head;
        return (int) Math.abs(t);
    }

    public static String parkTimes(String staTime, String endTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String parkTimes = null;

        try {
            Date d1 = df.parse(endTime);
            Date d2 = df.parse(staTime);
            long diff = d1.getTime() - d2.getTime();
            long days = diff / 86400000L;
            long hours = (diff - days * 86400000L) / 3600000L;
            long minutes = (diff - days * 86400000L - hours * 3600000L) / 60000L;
            parkTimes = "" + days + "天" + hours + "小时" + minutes + "分";
        } catch (Exception var14) {
            var14.printStackTrace();
        }

        return parkTimes;
    }

    public static String strCoverDateTime(String dateStr) {
        try {
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
            Date date = sdf1.parse(dateStr);
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf2.format(date);
        } catch (Exception var4) {
            var4.printStackTrace();
            return "";
        }
    }

    public static String formatDuring(long mss) {
        long days = mss / 86400000L;
        long hours = mss % 86400000L / 3600000L;
        long minutes = mss % 3600000L / 60000L;
        long seconds = mss % 60000L / 1000L;
        return days + " days " + hours + " hours " + minutes + " minutes " + seconds + " seconds ";
    }

    public static String cal(int second) {
        int h = 0;
        int d = 0;
        int s = 0;
        int temp = second % 3600;
        if (second > 3600) {
            h = second / 3600;
            if (temp != 0) {
                if (temp > 60) {
                    d = temp / 60;
                    if (temp % 60 != 0) {
                        s = temp % 60;
                    }
                } else {
                    s = temp;
                }
            }
        } else {
            d = second / 60;
            if (second % 60 != 0) {
                s = second % 60;
            }
        }

        return h + "时" + d + "分" + s + "秒";
    }

    public static String getNowDate() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
        String hehe = dateFormat.format(now);
        return hehe;
    }

    public static boolean thisIsBetween(String date1, String date2) {
        Date date = new Date();
        String[] format = new String[]{"yyyy-MM-dd"};
        boolean sks = false;

        try {
            Date datess = DateUtils.parseDate(date1, format);
            Date datess2 = DateUtils.parseDate(date2, format);
            sks = between(date, datess, datess2);
        } catch (ParseException var8) {
            var8.printStackTrace();
        }

        return sks;
    }

    public static int compare_date(String DATE1) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date dt1 = df.parse(DATE1);
            Date now = new Date();
            if (dt1.getTime() > now.getTime()) {
                System.out.println("dt1 在dt2前");
                return 1;
            } else if (dt1.getTime() < now.getTime()) {
                System.out.println("dt1在dt2后");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception var4) {
            var4.printStackTrace();
            return 0;
        }
    }

    public static Timestamp getNowTimestamp() {
        return Timestamp.valueOf(converDateToString(new Date()));
    }

    public static int getAge(Date birthDay) throws Exception {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException("The birthDay is before Now.It's unbelievable!");
        } else {
            int yearNow = cal.get(1);
            int monthNow = cal.get(2);
            int dayOfMonthNow = cal.get(5);
            cal.setTime(birthDay);
            int yearBirth = cal.get(1);
            int monthBirth = cal.get(2);
            int dayOfMonthBirth = cal.get(5);
            int age = yearNow - yearBirth;
            if (monthNow <= monthBirth) {
                if (monthNow == monthBirth) {
                    if (dayOfMonthNow < dayOfMonthBirth) {
                        --age;
                    }
                } else {
                    --age;
                }
            }

            return age;
        }
    }

    public static void main(String[] args) throws Exception {
        new Date();
        Timestamp timestamp = Timestamp.valueOf(converDateToString(new Date()));
        System.out.println(timestamp.toString());
    }
}
